<template>
  <div class="shopcar-container">
    <!-- 商品列表区域 -->
    <div class="goods_list">
      <div class="mui-card" v-for="(item, i) in goodsList" :key="(item.id, i)">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch v-model="$store.getters.getGoodsSelected[item.id]"
            @change="selectedChange(item.id,$store.getters.getGoodsSelected[item.id])"></mt-switch>
            <img :src="item.thumb_path" alt="">
            <div class="info">
              <h2>{{item.title}}</h2>
              <p>
                <span class="price">￥{{item.sell_price}}</span>
                <numbox :initCount = "$store.getters.getGoodsCount[item.id]" :goodsid = "item.id"></numbox>
                <a href="#" @click.prevent = "remove(item.id,i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总计(不含运费)</p>
            <p>已勾选商品<span class="red">{{$store.getters.getGoodsCountAndAmount.count}}</span>件，总价 ￥<span class="red">{{$store.getters.getGoodsCountAndAmount.amount}}</span></p>
          </div>
          <mt-button type = "danger">去结算</mt-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import numbox from '../subcomponents/shopcar_numbox.vue'
export default {
  data () {
    return {
      goodsList: []
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    getGoodsList () {
      // 获取store所有商品的id，拼接成字符串
      var idhrr = []
      this.$store.state.car.forEach(item => {
        idhrr.push(item.id)
      })
      if (idhrr.length <= 0) {
        return
      }
      this.$http.get('api/goods/getshopcarlist/' + idhrr.join(',')).then(result => {
        if (result.body.status === 0) {
          this.goodsList = result.body.message
        }
      })
    },
    remove (id, index) {
      // 点击删除把商品从store中数据传递的ID删除，把组件goodslist中的商品删除，通过Index
      this.goodsList.splice(index, 1)
      this.$store.commit('removeFormCar', id)
    },
    selectedChange (id, val) {
      // 每当点击开关，把数据同步到store
      this.$store.commit('updataGoodsSelected', { id, selected: val })
    }
  },
  components: {
    // eslint-disable-next-line vue/no-unused-components
    numbox
  }
}
</script>
<style lang="css" scoped>
.shopcar-container{
  background-color: #eee;
  overflow: hidden;
}
.shopcar-container .mui-card-content-inner{
  display: flex;
  align-items: center;
}
.shopcar-container .goods_list img{
  width: 60px;
  height: 60px;
}
.shopcar-container .goods_list h2{
  font-weight: bold;
  font-size: 13px;
}
.shopcar-container .goods_list .info {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.shopcar-container .goods_list .info .price{
  color: red;
  font-weight: bold;
}
.shopcar-container .goods_list .info p {
  display: flex;
}
.jiesuan {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.jiesuan .red{
  font-weight: bold;
  font-size: 16px;
  color: red;
  margin: 0px 4px;
}
</style>
